<?php
/*
 *   This file is part of poat.
 *
 *   poat is free software: you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation, either version 3 of the License, or
 *   (at your option) any later version.
 *
 *   poat is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with poat.  If not, see <http://www.gnu.org/licenses/>.
 */
 
require_once(ROOT_PATH."class/user.class.php");
require_once(ROOT_PATH."class/team.class.php");

require_once (ROOT_PATH."xajax_core/xajax.inc.php");

$xajax = new xajax("xr.php");
$xajax->registerFunction("sendMessage");
$xajax->registerFunction("updateUser");
//$xajax->setFlag("debug",true);
//$xajax->setFlag("statusMessages",true);

$team = new team;

if ($CURRENT_USER->groupid == GROUP_ADMIN) {
	$teamlist = $loader->loadList($team);	
} else {
	$team = new team;
	$team->id = $CURRENT_USER->teamid;
	$loader->get($team);
	$teamlist = array($team);
	$teamobj=$team;
}

if ($param['fteamid']!='') {
	foreach ($teamlist as $key => $obj) {
		if ($obj->id == $param['fteamid']) {
			$teamobj=$obj;	
		}	
	}
}

if ($param['op']=='act' or $param['op']=='deact' and $flow_ok) {

	foreach($param['userid'] as $id) {
		$us = new user;
		$us->userid=$id;
		$ret = $loader->get($us);
		if ($ret) {
			if ($param['op']=='act') {
				$us->status=STATUS_ACTIVE;
			} else {
				$us->status=STATUS_INACTIVE;
			}
			$loader->update($us);
			sendUserStatusMail($us);
		}
	}
	$message=$LANG['userupdated'];
	
} elseif ($param['op']=='chgrp' and $flow_ok) {
	$us = new user;
	$us->userid=$param['userid'];
	$ret = $loader->get($us);
	if ($ret) {
		$us->groupid=$param['groupid'];
		$loader->update($us);
		$message=$LANG['userupdated'];
	} else {
		$error=$LANG['updateerr'];	
	}
} elseif ($param['op']=='chteam' and $flow_ok) {
	$us = new user;
	$us->userid=$param['userid'];
	$ret = $loader->get($us);
	if ($ret) {
		$us->teamid=$param['teamid'];
		$loader->update($us);
		$message=$LANG['userupdated'];
		switchTeamMail($us);
	} else {
		$error=$LANG['updateerr'];	
	}
} elseif ($param['op']=='reset' and $flow_ok and $isadmin) {
	
	foreach($param['userid'] as $id) {
		$us = new user;
		$us->userid=$id;
		$ret = $loader->get($us);
		if ($ret) {
			$newpass = $us->userid;
			$us->password=$newpass;
			$loader->update($us);
		}
	}
	$message=$LANG['userupdated'];
	
} elseif ($param['op']=="remove" and $flow_ok and $isadmin) {
	foreach($param['userid'] as $id) {
		$us = new user;
		$us->userid=$id;
		$loader->delete($us);
	}
	$message=$LANG['userupdated'];
}

$us = new user;
$us->groupid=$param['fgroupid'];
if ($CURRENT_USER->groupid == GROUP_ADMIN) {
	$us->teamid=$param['fteamid'];	
} else {
	$us->teamid=$CURRENT_USER->teamid;
}
$us->status=$param['fstatusid'];
$list = $loader->loadListBy($us,$us->listByFilters());

foreach ($list as $key => $obj) {
	foreach ($teamlist as $tkey => $tobj) {
		if ($obj->teamid == $tobj->id) {
			$obj->team=$tobj;	
		}	
	}
}

$tm->assign('script',$xajax->getJavascript('scripts'));
$tm->assign('groupid',$us->groupid);
$tm->assign('teamid',$us->teamid);
$tm->assign('statusid',$us->status);

$tm->assign('statuslist',$STATUS_LIST);
$tm->assign('grouplist',$GROUP_LIST);
$tm->assign('teamlist',$teamlist);
$tm->assign('team',$teamobj);

$tm->assign('list',$list);
$content = $tm->fetch('userlist.tpl');

?>
